package com.renren.rrwx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.renren.mobile.rmsdk.core.RMConnectCenter;
import com.renren.mobile.rmsdk.core.RMConnectCenter.LoginListener;
import com.renren.mobile.rmsdk.core.exception.RRException;
import com.renren.mobile.rmsdk.friends.GetFriendsRequest;
import com.renren.mobile.rmsdk.friends.GetFriendsResponse;
import com.renren.mobile.rmsdk.friends.GetFriendsResponse.FriendItem;
import com.renren.rrwx.config.Config;
import com.renren.rrwx.model.HeroManager;

/**
 * 人人武侠的初始界面，使用人人社交组件tiny_client登陆，初始化数据
 * 
 * @author Leon
 * 
 */
public class RenrenWuXiaActivity extends Activity {

	public static String TAG = "WuXiaActivity";

	/**
	 * 人人微客户端核心类
	 */
	private ProgressBar loadPb;
	private TextView loadTv;
	private Button loginBtn;
	private Button startBtn;

	RMConnectCenter center;
	private boolean hasLoaded = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		center = RMConnectCenter.getInstance(this);
		center.setClientInfo(Config.API_KEY, Config.SECRET_KEY, Config.APP_ID);

		// 初始化UI控件
		loadPb = (ProgressBar) findViewById(R.id.main_load_progress);
		loadPb.setVisibility(View.INVISIBLE);
		loadTv = (TextView) findViewById(R.id.main_load_text);
		loadTv.setVisibility(View.INVISIBLE);
		startBtn = (Button) findViewById(R.id.main_start_btn);
		startBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!hasLoaded) {
					loadPb.setVisibility(View.VISIBLE);
					loadTv.setText(R.string.main_load);
					loadTv.setVisibility(View.VISIBLE);
					startBtn.setVisibility(View.INVISIBLE);
					// 重新加载
					initGameData();
				} else
					beginGame();
			}

		});

		loginBtn = (Button) findViewById(R.id.login_btn);
		// 否则显示登录按钮
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RenrenWuXiaActivity.this.loginRenren();
			}
		});
		this.initMainUI();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 回复当前UI
		initMainUI();
	}

	/**
	 * 根据当前状态，初始化UI
	 */
	private void initMainUI() {
		hasLoaded = false;
		loadPb.setVisibility(View.INVISIBLE);
		loadTv.setVisibility(View.INVISIBLE);
		// 如果已经登录，则直接跳转主界面
		if (center.hasLogin()) {
			// 如果已经登陆了
			loginBtn.setVisibility(View.INVISIBLE);
			// 此时则需要显示startBtn
			startBtn.setVisibility(View.VISIBLE);
		} else {
			// 未登录显示登录按钮
			loginBtn.setVisibility(View.VISIBLE);
			startBtn.setVisibility(View.INVISIBLE);
		}
	}

	private void initGameData() {
		// 初始化游戏数据，完成后更新当前状态
		new Handler().postDelayed(new Runnable() {
			public void run() {
				try {

					HeroManager hm = HeroManager
							.getInstance(RenrenWuXiaActivity.this);

					hm.initRenrenData();
					// 完成加载后
					hasLoaded = true;
					beginGame();
				} catch (RRException e) {
					LOGD("读取朋友列表失败!!! e msg=" + e.getMessage());
					e.printStackTrace();
					// 设置开始按钮为可见，并更换文字
					hasLoaded = false;
					startBtn.setText(R.string.main_reload);
					loadPb.setVisibility(View.INVISIBLE);
					loadTv.setText("数据加载失败，请重试\n");
					loadTv.setVisibility(View.VISIBLE);
					startBtn.setVisibility(View.VISIBLE);
					Toast.makeText(RenrenWuXiaActivity.this, e.getMessage(), 0)
							.show();
				}

			}
		}, 1000);
	}

	private void beginGame() {
		Intent i = new Intent(RenrenWuXiaActivity.this, GameActivity.class);
		RenrenWuXiaActivity.this.startActivity(i);
	}

	private void startVirtualGame() {
		// 使用虚拟数据开始游戏
		LOGD("Start a virtual Game!!");

	}

	/**
	 * 尝试用人人网帐号登录
	 */
	private void loginRenren() {
		Log.d(TAG, "Try to login!");
		center.setLoginListener(new LoginListener() {

			@Override
			public void onLoginCanceled() {
				// TODO Auto-generated method stub
				Log.d(TAG, "Login canceled!!!!!!!!!!!!!!!!!");
			}

			@Override
			public void onLoginSuccess() {
				Log.d(TAG, "Login success!!!!!!!!!!!!!!!!!!!");
			}

		});
		center.login(this);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_OK: {
			try {
				center.onActivityResult(requestCode, resultCode, data);
			} catch (Exception e) {
				e.printStackTrace();
			}
			initMainUI();
			break;
		}
		case RESULT_CANCELED: {

			break;
		}
		}
	}

	public static void LOGD(String msg) {
		Log.d(TAG, msg);
	}
}
